package com.david.controller;

import com.david.pojo.User;
import com.david.service.IGoodsService;
import com.david.service.IUserService;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.thymeleaf.util.StringUtils;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @program: Secondskill-Demo
 * @description: 商品Controller
 * @author: David
 * @version:
 * @create: 2022-04-13 15:06
 **/
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private IUserService userService;
    @Resource
    private IGoodsService goodsService;


    /**
     * 跳转商品列表页面
     * @param
     * @param model
     * @param ticket
     * @return
     */
    @RequestMapping("/toList")
    public ModelAndView toList(HttpServletRequest request, HttpServletResponse response, Model model, @CookieValue("userTicket") String ticket){
        ModelAndView view = new ModelAndView();
        if(StringUtils.isEmpty(ticket)){
            view.setViewName("login");
        }
        //User user = (User) session.getAttribute(ticket);
        User user = userService.getUserByCookie(ticket,request,response);
        if(null == user){
            view.setViewName("login");
            model.addAttribute("user", user);
            model.addAttribute("goodsList",goodsService.findGoodsVo());
        }
        view.setViewName("goodsList");
        return view;
    }
}
